﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaobaoERP.Entity;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Reflection;

namespace TaobaoERP.BusinessTaobao.Common
{
    /// <summary>
    /// 淘宝API xml处理程序
    /// </summary>
    public class XmlHandler<TEntity> where TEntity : new()
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="selectNodes">"rsp/item_cat"</param>
        /// <param name="par">key=实体 value=xml</param>
        /// <returns></returns>
        public List<TEntity> ListItemByXml(string result,string selectNodes,NameValueCollection par)
        {
            string itemcats = result;
            List<TEntity> list = new List<TEntity>();
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.LoadXml(itemcats);
            System.Xml.XmlNodeList xn = xmlDoc.SelectNodes(selectNodes);

            foreach (System.Xml.XmlNode xnl in xn)
            {

                TEntity item = new TEntity();
                foreach (string parKey in par.Keys)
                {

                    System.Reflection.PropertyInfo propertyInfo =item.GetType().GetProperty(parKey);
                    bool isNullableValueType = propertyInfo.PropertyType.IsGenericType && (propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>));
                    Type genericType = propertyInfo.PropertyType;

                    if (isNullableValueType)
                        genericType = propertyInfo.PropertyType.GetGenericArguments()[0];

                    string tempText = xnl.SelectSingleNode(par[parKey]).FirstChild.Value;
                    if (!string.IsNullOrEmpty(tempText))
                        propertyInfo.SetValue(item, Convert.ChangeType(tempText, genericType), null);
                }
                list.Add(item);
                
            }
            return list;
        }

        public TEntity GetItemByXml(string result, string selectNodes, string par)
        {
            string itemcats = result;
            TEntity entity = default(TEntity);
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.LoadXml(itemcats);
            System.Xml.XmlNodeList xn = xmlDoc.SelectNodes(selectNodes);
            Type t=typeof(TEntity);
            foreach (System.Xml.XmlNode xnl in xn)
            {
                string tempText = xnl.SelectSingleNode(par).FirstChild.Value;
                return (TEntity)Convert.ChangeType(tempText, typeof(TEntity));
            }
            return entity;
        }

        public TEntity GetItemByXml(string result, string selectNodes, NameValueCollection par)
        {
            string itemcats = result;
            TEntity entity = default(TEntity);
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            xmlDoc.LoadXml(itemcats);
            System.Xml.XmlNodeList xn = xmlDoc.SelectNodes(selectNodes);
            foreach (System.Xml.XmlNode xnl in xn)
            {

                TEntity item = new TEntity();
                foreach (string parKey in par.Keys)
                {

                    System.Reflection.PropertyInfo propertyInfo = item.GetType().GetProperty(parKey);
                    bool isNullableValueType = propertyInfo.PropertyType.IsGenericType && (propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>));
                    Type genericType = propertyInfo.PropertyType;

                    if (isNullableValueType)
                        genericType = propertyInfo.PropertyType.GetGenericArguments()[0];

                    string tempText = xnl.SelectSingleNode(par[parKey]).FirstChild.Value;
                    if (!string.IsNullOrEmpty(tempText))
                        propertyInfo.SetValue(item, Convert.ChangeType(tempText, genericType), null);
                }
            }
            return entity;
        }
    }
}
